//
//  HLNIMUserCache.m
//  YueYue
//
//  Created by hulu001 on 2024/1/11.
//

#import "HLNIMUserCache.h"

@implementation HLNIMUserCache

+ (instancetype)shareInstance
{
    static HLNIMUserCache * __shareInstance = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        __shareInstance = [[HLNIMUserCache alloc] init];
    });
    
    return __shareInstance;
}

- (NSMutableDictionary *)usersCache
{
    if (!_usersCache) {
        _usersCache = [NSMutableDictionary dictionary];
    }
    return _usersCache;
}

- (void)userInfor:(NSString *)userId completion:(nonnull void (^)(NIMUser * _Nonnull))completion
{
    NIMUser *user = [NIMSDK.sharedSDK.userManager userInfo:userId];
    if (user.userInfo.nickName) {
        completion(user);
        return;
    }
    
    if (self.usersCache[userId]) {
        completion(self.usersCache[userId]);
        return;
    }
    // 云信获取用户信息
    [NIMSDK.sharedSDK.userManager fetchUserInfos:@[userId] completion:^(NSArray<NIMUser *> * _Nullable users, NSError * _Nullable error) {
        self.usersCache[userId] = users.firstObject;
        completion(users.firstObject);
    }];
}

- (void)clear
{
    [self.usersCache removeAllObjects];
}


@end
